using System;
using System.Globalization;
using System.Reflection.Emit;

namespace RaisingStudio.SmallBasicCompiler.Expressions
{
	[Serializable]
	public class LiteralExpression : Expression
	{
		public TokenInfo Literal
		{
			get;
			set;
		}
		public override void EmitIL(CodeGenScope scope)
		{
			if (this.Literal.Token == Token.StringLiteral)
			{
				scope.ILGenerator.Emit(OpCodes.Ldstr, this.Literal.Text.Trim(new char[]
				{
					'"'
				}));
				scope.ILGenerator.EmitCall(OpCodes.Call, scope.TypeInfoBag.StringToPrimitive, null);
				return;
			}
			if (this.Literal.Token == Token.NumericLiteral)
			{
				scope.ILGenerator.Emit(OpCodes.Ldc_R8, double.Parse(this.Literal.Text, CultureInfo.InvariantCulture));
				scope.ILGenerator.EmitCall(OpCodes.Call, scope.TypeInfoBag.NumberToPrimitive, null);
			}
		}
		public override string ToString()
		{
			return this.Literal.Text;
		}
	}
}
